package com.wcsy.myblog.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcsy.myblog.dao.TypeMapper;
import com.wcsy.myblog.dao.UserMapper;
import com.wcsy.myblog.entity.Type;
import com.wcsy.myblog.entity.User;
import com.wcsy.myblog.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper,Type> implements ITypeService {
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private NumServiceImpl numService;
    @Autowired
    HttpSession session;
    @Autowired
    UserMapper userMapper;


    @Override
    public int insertType(String typeName, String typeKidName) {
        int temp =0;
               User user = (User) session.getAttribute("user");
//        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_id", 1));
        //判断文章类型是不是敏感词
        if (!typeName.equals("-1")) {
            String getfilterTypeName = numService.getfilter(typeName);
            if (!typeName.equals(getfilterTypeName)) {
                return -1;
            }
        }
        if (!typeKidName.equals("-1")) {
            String getfilterTypeKidName = numService.getfilter(typeKidName);
            if (!typeKidName.equals(getfilterTypeKidName)) {

                return -1;
            }
        }
        Type type = new Type();
        //获取时间
        Date date = new Date(System.currentTimeMillis());
        String format = new SimpleDateFormat("YYYY年MM月dd日HH:mm").format(date);
        //查询出该用户的文章类型信息
        List<Type> types = typeMapper.selectTypeList((int) user.getUserId());
        //临时存储类型名称的集合
        ArrayList<String> stringTypeName = new ArrayList<>();
        if (!types.isEmpty()) {
            for (int i = 0; i < types.size(); i++) {
                stringTypeName.add(types.get(i).getTypeName());
            }
        }
        if (!typeName.equals("-1") && !typeKidName.equals("-1")) {
            //如果父子级类型都在types中
            if (stringTypeName.contains(typeName) && stringTypeName.contains(typeKidName)) {
                //根据父级类型在数据库中查出一个集合
                List<Type> typeNameList = typeMapper.selectTypeName(typeName, (int) user.getUserId());
                //根据子级类型在数据库中查出一个集合
                List<Type> typeKidNameList = typeMapper.selectTypeName(typeKidName, (int) user.getUserId());
                Boolean tp = true;
                for (int i = 0; i < typeNameList.size(); i++) {
                    for (int i1 = 0; i1 < typeKidNameList.size(); i1++) {
                        if (typeNameList.get(i).getTypeId() == typeKidNameList.get(i1).getTypePid()) {
                            System.out.println("该子类型已是父类型的子类型，无需添加");
                            temp = (int) typeKidNameList.get(i1).getTypePid();
                            tp = false;
                        }
                    }
                }
                if (tp) {
                    //循环完后如果不存在父类型的id与子类型的pid对应，则添加子类型且其pid为父类型的id
                    type.setTypeName(typeKidName);
                    type.setTypeTime(format);
                    type.setTypePid(typeNameList.get(0).getTypeId());
                    type.setUserId((int) user.getUserId());
                    typeMapper.insertType(type);
                    List<Type> types1 = typeMapper.selectTypeList((int) user.getUserId());
                    temp = (int) (types1.get(types1.size() - 1).getTypeId());
                }
                //如果父级在数据库中，子级不在,则新增子级  且pid是父级id
            } else if (stringTypeName.contains(typeName) && !stringTypeName.contains(typeKidName)) {
                List<Type> typeNameList = typeMapper.selectTypeName(typeName, (int) user.getUserId());
                type.setTypeName(typeKidName);
                type.setTypeTime(format);
                type.setTypePid(typeNameList.get(0).getTypeId());
                type.setUserId((int) user.getUserId());
                typeMapper.insertType(type);
                List<Type> types1 = typeMapper.selectTypeList((int) user.getUserId());
                temp = (int) (types1.get(types1.size() - 1).getTypeId());
                //如果子级在数据库中，父级不在，则父子级都需要添加
            } else if (!stringTypeName.contains(typeName)) {
                //添加父级
                type.setTypeName(typeName);
                type.setTypeTime(format);
                type.setTypePid(0L);
                type.setUserId((int) user.getUserId());
                typeMapper.insertType(type);
                //查询出添加完成后父级的id  当成子级的pid
                List<Type> types1 = typeMapper.selectTypeList((int) user.getUserId());
                //添加子级
                type.setTypeName(typeKidName);
                type.setTypeTime(format);
                type.setTypePid(types1.get(types1.size() - 1).getTypeId());
                type.setUserId((int) user.getUserId());
                typeMapper.insertType(type);
                types1 = typeMapper.selectTypeList((int) user.getUserId());
                temp = (int) (types1.get(types1.size() - 1).getTypeId());
            }
            //如果父级不为null，子级为null，则添加父级
        } else if (!typeName.equals("-1") && typeKidName.equals("-1")) {
            if (!stringTypeName.contains(typeName)){
                type.setTypeName(typeName);
                type.setTypeTime(format);
                type.setTypePid(0L);
                type.setUserId((int) user.getUserId());
                typeMapper.insertType(type);
                List<Type> types1 = typeMapper.selectTypeList((int) user.getUserId());
                temp = (int) (types1.get(types1.size() - 1).getTypeId());
            }else {
                //根据name查出id
                List<Type> types1 = typeMapper.selectTypeName(typeName, (int) user.getUserId());
                temp = (int) types1.get(0).getTypeId();
            }
            // 如果父级为null，子级不为null，则添加子级
        } else {
            if (!stringTypeName.contains(typeKidName)){
                type.setTypeName(typeKidName);
                type.setTypeTime(format);
                type.setTypePid(0L);
                type.setUserId((int) user.getUserId());
                typeMapper.insertType(type);
                List<Type> types1 = typeMapper.selectTypeList((int) user.getUserId());
                temp = (int) (types1.get(types1.size() - 1).getTypeId());
            }else {
                //根据name查出id
                List<Type> types1 = typeMapper.selectTypeName(typeKidName, (int) user.getUserId());
                temp = (int) types1.get(0).getTypeId();
            }
        }
        return temp;
    }

    @Override
    public List<Type> selectOneList(int userId) {
        //最终返回的集合
        List<Type> list = new ArrayList<>();
        //避免pid重复
        ArrayList<Integer> integers = new ArrayList<>();
        //查询出该用户的文章类型信息
        List<Type> types = typeMapper.selectTypeList(userId);
        if (!types.isEmpty()){
            //将不重复的pid存储到integers集合中
            for (int i = 0; i < types.size(); i++) {
                if (!integers.contains((int)types.get(i).getTypePid())){
                    integers.add((int)types.get(i).getTypePid());
                }
            }
            for (int i = 0; i < types.size(); i++) {
                    for (int j = 0; j < integers.size(); j++) {
                            //如果找到两个类型 且父类型的id  等于子类型的pid
                            if (types.get(i).getTypeId()==integers.get(j)){
                                //根据子类型的pid查出子类型集合
                                List<Type> pidType = typeMapper.selectPidType(userId, integers.get(j));
                                //根据父类型的id查出类型，添加到父类型
                                Type type = typeMapper.selectIdType((int) types.get(i).getTypeId());
                                type.setChildren(pidType);
                                list.add(type);
                            }
                    }
            }

            //根据子类型的pid=0且id不等于任何一个人的pid的查出子类型集合
            List<Type> pidType = typeMapper.selectPidType(userId,0);
            for (int i = 0; i < pidType.size(); i++) {
                 if (!integers.contains((int)pidType.get(i).getTypeId())){
                     list.add( pidType.get(i));
                 }
            }
        }
        return list;
    }
}
